$(function() {
    //鼠标移入移除事件
    $.get("http://127.0.0.1:8400/jumeiyoupin/homepage", {
        "inite_homepage": true
    }, function(data) {
        //从服务端获取商品列表并添加到页面
        var today_new_ul = "";
        $.each(data, function(index, element) {
            today_new_ul += `
                <li class="newdeal_box">
                    <div class="img_box">
                        <a class="img_box_href" target="_blank" href="` + element.urlAddr + `">
                            <img src="` + element.img + `" class="img_w1000 all_cart_img">
                            <div class="commit_new" style="display: none;">
                                <div class="commit_new_box commit_new_global">海外直采 海外价格 闪电发货</div>
                            </div>
                        </a>
                        <div class="add_cart_box all_cart_wrap" style="display: none;">
                            <a target="_self" class="add_cart all_cart">加入购物车</a>
                        </div>
                    </div>
                    <a target="_blank" href="` + element.urlAddr + `">
                        <div class="today_new_detail">
                            <p class="title">` + element.introduction + `</p>
                            <div class="intro_box">
                                <div class="price_box">
                                    <em>¥</em>
                                    <span class="pnum">` + element.price + `</span>
                                </div>
                            </div>
                        </div>
                    </a>
                </li>
            `
        })
        $("#today_new_ul").html(today_new_ul)
        var $new_li_item = $(".newdeal_box");
        var lievens = {
            mouseenter: function(event) {
                var ele = event.target;
                $(ele).find(".commit_new").css("display", "block");
                $(ele).find(".add_cart_box").css("display", "block");
            },
            mouseleave: function(event) {
                var ele = event.target;
                $(ele).find(".commit_new").css("display", "none");
                $(ele).find(".add_cart_box").css("display", "none");
            }
        }
        $new_li_item.on(lievens);
    }, "JSON");

    //等待页面加载完成之后执行的函数
    setTimeout(function() {
        var $add_cart_box = $(".add_cart_box");
        $add_cart_box.on("click", function(event) {
            var ele = event.target;
            var img_src = $(this).siblings(".img_box_href").find(".img_w1000").attr("src");
            console.log(img_src);
            axios({
                method: "get",
                url: "http://127.0.0.1:8400/goShopping",
            }).then(response => {
                let bool = response.data;
                if (bool != false) {
                    console.log(bool);
                    $.get("http://127.0.0.1:8400/jumeiyoupin/homepage", {
                        "add_shoppingcar": img_src
                    }, "JSON");
                } else {
                    window.location = "../src/登录.html";
                }
            })
        })
    }, 100)
})


/* 校验是否登陆  拿到那个用户 */
axios({
    method: "get",
    url: "http://127.0.0.1:8400/goShopping",
}).then(response => {
    let bool = response.data;

    if (bool != false) {
        let a = document.getElementById("a1");
        a.innerHTML = bool.admin;
        a.style.color = "red";
        a.setAttribute("href", "../src/我的聚美.html");
        $('.a2').css("display", "none");
        $(".span").css("display", "inline-block");
        console.log($(".span"));
    }
})

function exit() {
    let a = document.getElementById("a1");
    a.innerHTML = "请登录";
    a.style.color = "black";
    $(".span").css("display", "none");
    $('.a2').css("display", "block");
    axios({
        method: "get",
        url: "http://127.0.0.1:8400/exit",

    }).then(response => {
        window.location = "../homepage.html";
    })
}
/* 校验登陆用户 */
function goShopping() {
    axios({
        method: "get",
        url: "http://127.0.0.1:8400/goShopping",

    }).then(response => {
        let bool = response.data;
        if (bool != false) {
            window.location = "../shoppingcar.html";
        } else {
            window.location = "../src/登录.html";
        }
    })
}